<template>
  <div class="tpl-message">
    <Card :dis-hover="true">
      <div id="content">
        <Form ref="form_data" :model="form_data" :rules="rule_form_data">
          <FormItem label="订阅消息-审核结果通知模板id(请在公众号内请先设置服务类目为：IT科技>软件服务提供商)" prop="result_check_sub">
            <Input v-model="form_data.result_check_sub" placeholder="订阅消息-审核结果通知模板id"></Input>
          </FormItem>

          <FormItem>
<!--            <Button type="primary" @click="submit('form_data')">确认设置</Button>-->
            <Button type="primary" @click="autoSet">一键配置</Button>
          </FormItem>
        </Form>
      </div>
    </Card>
  </div>
</template>

<script>
export default {
  name: 'sub-message',
  data () {
    return {
      form_data: {
        result_check_sub: ''
      },
      rule_form_data: {
        result_check_sub: [
          { required: true, message: '请设置模板消息id', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    data () {
      let { result_check_sub } = this.form_data
      return {
        result_check_sub
      }
    }
  },
  mounted () {
    this.find()
  },
  methods: {
    // Run
    /**
     * 获取参数
     */
    find () {
      this.$get('settingMessage/find')
        .then((res) => {
          let { code, data } = res.data
          if (code === 0) {
            let { result_check_sub } = data
            this.form_data = {
              result_check_sub
            }
          }
        })
    },
    /**
     * 提交设置参数
     */
    submit (name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          this.$post('settingMessage/set', {
            data: this.data
          })
            .then((res) => {
              if (res.data.code === 0) {
                this.$Message.success('设置成功')
              } else {
                this.$Message.error('设置失败')
              }
            })
        }
      })
    },
    autoSet() {
      this.$post('settingMessage/autoSetSubscribeMessage')
        .then((res) => {
          let { code, data, err_msg } = res.data
          if (code === 0) {
            this.form_data = {
              result_check_sub: data
            }
          } else {
            this.$Message.error(err_msg)
          }
        })
    }
  }
}
</script>

<style scoped lang="less">
  #content{
    width: 600px;
  }
</style>
